/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright  Copyright (c) 2006-2007, Julien PORTALIER
 * @link       http://feather-cm.googlecode.com/
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

if (!Form)
	var Form = {};

Form.Help = new Class(
{
	initialize: function(form)
	{
		$ES('.required, .optional', form).each(function(el)
		{
			var label = $E('label', el);
			if (label && label.hasAttribute('title'))
			{
				var help = label.getAttribute('title');
				var input = $ES('input,select,textarea', el)[0];
				
				input.addEvent('focus', this.show.bindWithEvent(this));
				input.addEvent('blur',  this.hide.bindWithEvent(this));
			}
		}, this); 
	},
	
	show: function(evt)
	{
		var label = $E('label', evt.target.parentNode);
		var help  = label.getAttribute('title');
		help = help.replace(/\*(.+?)\*/, '<strong>$1</strong>');
		
		this.element = new Element('div', {
			'class': 'inline-help',
			'styles': {
				'position': 'absolute',
				'left': evt.target.parentNode.offsetLeft + evt.target.parentNode.offsetWidth + 'px'
			}
		});
		this.element.appendChild(new Element('p').setHTML(help));
		this.element.injectBefore(evt.target.parentNode);
	},
	
	hide: function(evt)
	{
		if (this.element)
		{
			this.element.remove();
			this.element = null;
		}
	}
});
/*
Window.onDomReady(function() {
	$ES('form').each(function(form) { new Form.Help(form); });
});
*/